package com.ze.familydayverpm.fragment;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.ze.commontool.DisplayUtil;
import com.ze.commontool.LoadImageMgr;
import com.ze.commontool.NetHelper;
import com.ze.commontool.PublicInfo;
import com.ze.commontool.LoadImageMgr.ImageCallBack;
import com.ze.commontool.StringTools;
import com.ze.familyday.familyphotoframesquare.PhotoFrameActivity;
import com.ze.familyday.familyphotoframesquare.R;
import com.ze.model.PhotoModel;
import com.ze.model.PhotoModel.PicInfo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SubscribeFragment extends RelativeLayout  implements RelativeLayoutImp {
	
	private 	Context mContext;
	/**
	 * copy from mouse 
	 */
	
	public View picView;
	
	/**
	 * 字体处理
	 */
	TextPaint newPaint_name, newPaint_time, newPaint_say, newPaint_saybody;
	/**
	 * 各字体View
	 */
	TextView new_name_back, new_name, new_time_back, new_time, new_say_back,
				new_say;
	
	/**
	 * 订阅号图片
	 */
	private ImageView subscribeIv;
	/**
	 * 订阅号内容
	 */
	private WebView subscribeWv;
	/**
	 * 数据
	 */
	private LinearLayout subLl;
	private LinearLayout mainLl;
//	private List<JSONObject> mListJSON;
	private 	PhotoModel mObject;
	public static final String 	IMG_DETAIL_TAIL = "!580";
	public static final int 		ONCE_GET = 5;
	boolean isLandscape = false;
	
	public SubscribeFragment(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		mContext = context;
//		setupViews();
	}
	 public SubscribeFragment(Context context, AttributeSet attrs) {  
	        super(context, attrs);  
	        setupViews();  
	    } 
	//初始化View
    public void setupViews(){  
    }  
    public void pageDown()
    {
    }
    public void pageUp()
    {
    }
    private float touchY;
    /** 
     * 填充数据，共外部调用. 
     * @param object 
     */  
    public void setData(PhotoModel object){ 
        this.mObject = object; 
//        ArrayList<PicInfo> 	imgArray = null;
        if ( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
		{
			isLandscape = true;
		}else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
		{
			isLandscape = false;
		}
        
        if (object.pic != null) {
			if (isLandscape) {
				int random = (int) (Math.random()*10);
				if (random % 2 == 0) {
					picView = LayoutInflater.from(mContext).inflate(R.layout.pic_subscribe_left, null);
				}else {
					picView = LayoutInflater.from(mContext).inflate(R.layout.pic_subscribe_right, null);
				}
			}else {
				picView = LayoutInflater.from(mContext).inflate(R.layout.pic_subscribe_port, null);
			}
			new_name_back = (TextView) picView.findViewById(R.id.new_desc_name_back);
			new_name = (TextView) picView.findViewById(R.id.new_desc_name);
			new_time_back = (TextView) picView.findViewById(R.id.new_desc_time_back);
			new_time = (TextView) picView.findViewById(R.id.new_desc_time);
			new_say_back = (TextView) picView.findViewById(R.id.new_desc_back);
			new_say = (TextView) picView.findViewById(R.id.new_desc);
			subscribeIv = (ImageView) picView.findViewById(R.id.iv);
			subLl = (LinearLayout) picView.findViewById(R.id.subLl);
			subLl.setOnClickListener(ButtonOnclickListener);
			mainLl = (LinearLayout) picView.findViewById(R.id.pslLl);
			mainLl.setOnClickListener(ButtonOnclickListener);
			subscribeWv = (WebView) picView.findViewById(R.id.wv);
			//设置webview属性
			//subscribeWv.setOnLongClickListener(onLongclicklistener);
			subscribeWv.setOnTouchListener(ontouchlistener);
			subscribeWv.getSettings().setJavaScriptEnabled(true);
			subscribeWv.getSettings().setSupportZoom(true);
			subscribeWv.getSettings().setBuiltInZoomControls(false);
			subscribeWv.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
			if (isLandscape) {
				//用户名
				new_name_back.setTextColor(getResources().getColor(R.color.picinfo_name));
				new_name.setTextColor(getResources().getColor(R.color.picinfo_name));
				newPaint_name = new_name_back.getPaint();
				newPaint_name.setStrokeWidth(0.0f);
				newPaint_name.setStyle(Style.STROKE);
				//时间
				new_time_back.setTextColor(getResources().getColor(R.color.picinfo_time));
				new_time.setTextColor(getResources().getColor(R.color.picinfo_time));
				newPaint_time = new_time_back.getPaint();
				newPaint_time.setStrokeWidth(0.0f);
				newPaint_time.setStyle(Style.STROKE);
				//图片内容
				newPaint_say = new_say_back.getPaint();
				newPaint_say.setStrokeWidth(0.0f);
				newPaint_say.setStyle(Style.STROKE);
			}else {
				//用户名
				new_name.setTextColor(Color.WHITE);
				newPaint_name = new_name_back.getPaint();
				newPaint_name.setStrokeWidth(1.0f);
				newPaint_name.setStyle(Style.STROKE);
				//时间
				new_time.setTextColor(Color.WHITE);
				newPaint_time = new_time_back.getPaint();
				newPaint_time.setStrokeWidth(1.0f);
				newPaint_time.setStyle(Style.STROKE);
				//图片内容
				new_say.setTextColor(Color.WHITE);
				newPaint_say = new_say_back.getPaint();
				newPaint_say.setStrokeWidth(1.0f);
				newPaint_say.setStyle(Style.STROKE);
			}
			
			
			picView.setOnClickListener(ButtonOnclickListener);
			addView(picView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			
			//Bitmap bitmap = null;
			try {
				String url = object.pic;
				subscribeWv.loadDataWithBaseURL("", object.message, "text/html", "UTF-8", "");
				bitmap = LoadImageMgr.getInstance().loadDrawble(new PicInfo(url, 512, 768),
						subscribeIv, 
						imageCallBack);
				if (bitmap != null) {
					subscribeIv.setImageBitmap(bitmap);
				}
				//ssobject.rececle = bitmap;
				PhotoFrameActivity.recycleBm.add(bitmap);
			}catch (Exception e) {
				e.printStackTrace();
			}
			setPicInfo(object);
		}
          
    }
    private Bitmap bitmap;
    public void setPicInfo(PhotoModel object)
	{
		if( object == null )
		{
			if (new_name_back!=null)
				new_name_back.setText("");
			if (new_name != null)
				new_name.setText("");
			if (new_say_back != null)
				new_say_back.setText("");
			if (new_say != null)
				new_say.setText("");
			if (new_time_back != null)
				new_time_back.setText("");
			if (new_time != null)
				new_time.setText("");
		}else
		{
			try {
				String str_say = object.say;
				if (str_say.equals("")) {
					str_say = "无标题";
				}
				new_say_back.setText(str_say);//say.setText( str_say);
				new_say.setText(str_say);//layout_info_say_back.setText( str_say );
				//layout_info_name_back.setText(object.getString("name"));
				//layout_info_name.setText(object.getString("name"));
				new_name_back.setText(object.name);
				new_name.setText(object.name);
				
				if(object.id.contains(PublicInfo.picID_default_head))
				{
					new_time.setText("");//layout_info_time.setText("");
					new_time_back.setText("");//layout_info_time_back.setText( "");
				}else
				{
					new_time.setText(NetHelper.transTime(Long.parseLong(object.time)) );
					new_time_back.setText( NetHelper.transTime(Long.parseLong(object.time)) );
				}
				
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
    public boolean isTextCut(TextView tv, String string)
    {
    	Paint paint  = tv.getPaint(); 
        paint.setTextSize(tv.getTextSize());
        // px = dp * (dpi / 160)
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int avail = PublicInfo.SCREEN_W - DisplayUtil.dip2px((40 + 18 + 10), dm.density);
    	String changeString = 
    		TextUtils.ellipsize(string, (TextPaint) paint, avail, TextUtils.TruncateAt.END).toString();
    	if (string.length() > changeString.length() ) {
			return true;
		}
    	return false;
    }
    /** 
     * 这里内存回收.外部调用. 
     */  
    
    public ImageCallBack imageCallBack = new ImageCallBack() {
		
		@Override
		public void setImage(Bitmap bitmap, String url, ImageView view) {
			// TODO Auto-generated method stub
			if( url.equals((String)view.getTag()))
			{
				view.setImageBitmap(bitmap);
			}
		}
	};
    public void recycle(){  
    	StringTools.rececleBm(bitmap);
    }  
    /** 
     * 重新加载.外部调用. 
     */  
    public void reload(){  
    	setData(mObject);
    }
    
    private boolean loading = false;
   
    private OnClickListener ButtonOnclickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
				( (PhotoFrameActivity)mContext ).stopOrContinue(
						! ((PhotoFrameActivity)mContext ).play_stop );
				
		}
	};
	
	private OnLongClickListener onLongclicklistener = new OnLongClickListener() {
		
		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			if (v == subscribeWv) {
				( (PhotoFrameActivity)mContext ).stopOrContinue(
						! ((PhotoFrameActivity)mContext ).play_stop );
			}
			return true;
		}
	};
	
	private OnTouchListener ontouchlistener = new OnTouchListener() {
		
		float mLastY = 0;
		float mCurrentY = 0;
		float mMoveY = 0;
		boolean flag = false;
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mLastY = mMoveY = event.getY();
				flag = false;
				subscribeWv.scrollTo(subscribeWv.getScrollX(), subscribeWv.getScrollY() + 1);
				break;
			case MotionEvent.ACTION_UP:
				mCurrentY = event.getY();
				if (Math.abs(mLastY - mCurrentY) < 10 && !flag) {
					( (PhotoFrameActivity)mContext ).stopOrContinue(
							! ((PhotoFrameActivity)mContext ).play_stop );
				}
				break;
			case MotionEvent.ACTION_MOVE:
				//float moveY = event.getY();
				mCurrentY = event.getY();
				if (Math.abs(mLastY - mCurrentY) >= 10) {
					mMoveY = mCurrentY;
					if (!flag) {
						flag = true;
					}
				}
				break;
			default:
				break;
			}
			return false;
		}
	};
	
	
	@Override
	public boolean isLoadDisscussed() {
		// TODO Auto-generated method stub
		return loading;
	}
	@Override
	public void loadDisscussTask() {
		// TODO Auto-generated method stub
		
	}
}
